package com.wwwarehouse.carddesk.fragment;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.CardDeskMYResponseBean;
import com.wwwarehouse.carddesk.bean.CardDeskMessageNoreadResponseBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.carddesk.fragment.mytabfragment.MyMessageQuitSystemFragment;
import com.wwwarehouse.carddesk.fragment.mytabfragment.MyOwnMessageFragment;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.CardDeskMyTabEvent;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.eventbus_event.MessageCenterReceivedEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

@Route(path = "/carddesk/DeskMyFragment")
public class DeskMyFragment extends BaseFragment implements View.OnClickListener {

    View v = null;
    private CircleImageView mDeskMyMainTopimage;
    private TextView mDeskMyMainToptext;
    private TextView mDeskMyMainTopphonetxt;
    private StateButton mDeskMyMainTopButton;
    private LinearLayout mDeskMyMainToplin;
    private ImageView mDeskMyMainCenterimg;
    private TextView mDeskMyMainCentermessagetxt;
    private TextView mDeskMyMainCenterNeedreadtxt;
    private LinearLayout mDeskMyMainCenterlin;
    private LinearLayout mDeskMyMainBottomlin;
    private ElasticScrollView mElasticScroll;
    private StateLayout mStateLayout;

    private static final int REQUEST_OPERATION_FAVORITE = 10003,REQUEST_OPERATION_NOREAD = 10004;
    private CardDeskMYResponseBean bean;

    private static Map<String,String> statusMap = new HashMap<>();
    private CardDeskMessageNoreadResponseBean noreadResponseBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_desk_my, container, false);
        initView(v);
        EventBus.getDefault().register(this);
        return v;
    }

    private void initView(View v) {
        mDeskMyMainTopimage = (CircleImageView) v.findViewById(R.id.desk_my_main_topimage);
        mDeskMyMainToptext = (TextView) v.findViewById(R.id.desk_my_main_toptext);
        mDeskMyMainTopphonetxt = (TextView) v.findViewById(R.id.desk_my_main_topphonetxt);
        mDeskMyMainTopButton = (StateButton) v.findViewById(R.id.desk_my_main_top_button);
        mDeskMyMainCenterimg = (ImageView) v.findViewById(R.id.desk_my_main_centerimg);
        mDeskMyMainCentermessagetxt = (TextView) v.findViewById(R.id.desk_my_main_centermessagetxt);
        mDeskMyMainCenterNeedreadtxt = (TextView) v.findViewById(R.id.desk_my_main_center_needreadtxt);
        mDeskMyMainCenterlin = (LinearLayout) v.findViewById(R.id.desk_my_main_centerlin);
        mDeskMyMainBottomlin = (LinearLayout) v.findViewById(R.id.desk_my_main_bottomlin);
        mDeskMyMainToplin = (LinearLayout) v.findViewById(R.id.desk_my_main_toplin);
        mElasticScroll = (ElasticScrollView) v.findViewById(R.id.elastic_scroll);
        mStateLayout = (StateLayout) v.findViewById(R.id.desk_my_main_statelayout);

        mDeskMyMainTopButton.setOnClickListener(this);
        mDeskMyMainToplin.setOnClickListener(this);
        mDeskMyMainCenterlin.setOnClickListener(this);
        mDeskMyMainBottomlin.setOnClickListener(this);

        statusMap.put("1",getString(R.string.taskdesk_my_status_1));
        statusMap.put("5",getString(R.string.taskdesk_my_status_5));

        mDeskMyMainTopButton.setVisibility(View.GONE);

        mDeskMyMainCenterNeedreadtxt.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG); //下划线

        mStateLayout.showProgressView(true);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
       if(bean!=null){
           if (i == R.id.desk_my_main_toplin) {
               if("1".equals(bean.getStatus() + "")){ // 审核通过可以点击
                   pushFragment(new MyOwnMessageFragment());
               }else{

               }
           }
       }

        if (i == R.id.desk_my_main_centerlin) {//跳转到消息框架

            pushFragment("/app_module/NotificationListFragment",null);

        } else if (i == R.id.desk_my_main_bottomlin) {
            pushFragment(new MyMessageQuitSystemFragment());
        }
    }

    @Override
    public void setTitle() {

    }

    private void updatePage(){
        if(bean!=null){
            BaseApplication.getApplicationInstance().displayImg(bean.getFaceUrl(),mDeskMyMainTopimage);
            mDeskMyMainToptext.setText("" + bean.getUserAccount());

            if(bean.getMobile() != null && bean.getMobile().length() == 11){
                mDeskMyMainTopphonetxt.setText("" + bean.getMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
            }


            if("1".equals(bean.getStatus() + "")){
                mDeskMyMainTopButton.setVisibility(View.GONE);
            }else{
                mDeskMyMainTopButton.setVisibility(View.VISIBLE);
                mDeskMyMainTopButton.setText("" + statusMap.get(bean.getStatus()+""));
            }
        }
    }

    @Override
    public void requestDatas() {
        Map map = new HashMap();
        NoHttpUtils.httpPost(CardDeskConstant.URL_MYMESSAGE, map, onResponseListener, REQUEST_OPERATION_FAVORITE);

        NoHttpUtils.httpPost(CardDeskConstant.URL_MY_GET_NEWINFOMESSAGE, map, onResponseListener, REQUEST_OPERATION_NOREAD);
    }



    NoHttpUtils.OnResponseListener onResponseListener= new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            switch (i){
                case REQUEST_OPERATION_FAVORITE:
                    if("0".equals(commonClass.getCode())){
//                        LogUtils.showErrLog("我的框架返回" + commonClass.getData());
                        bean = JSON.parseObject(commonClass.getData().toString(),CardDeskMYResponseBean.class);
                        updatePage();
                    }else {
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                requestDatas();
                            }
                        });
                    }
                    break;
                case REQUEST_OPERATION_NOREAD:
                    if("0".equals(commonClass.getCode())){
                        noreadResponseBean = JSON.parseObject(commonClass.getData().toString(),CardDeskMessageNoreadResponseBean.class);

                        if(noreadResponseBean.getCount() == 0 ){
                            mDeskMyMainCentermessagetxt.setText("" + getString(R.string.taskdesk_my_message_nohavemessage));
                            mDeskMyMainCenterNeedreadtxt.setVisibility(View.INVISIBLE);
                        }else{
                            if(StringUtils.isNullString(noreadResponseBean.getLastTime())){
                                mDeskMyMainCentermessagetxt.setText("" +  getString(R.string.taskdesk_my_minutesago_txt));
                            }else{
                                mDeskMyMainCentermessagetxt.setText("" + noreadResponseBean.getLastTime() +  getString(R.string.taskdesk_my_minutesago_txt));
                            }
                            mDeskMyMainCenterNeedreadtxt.setVisibility(View.VISIBLE);
                            mDeskMyMainCenterNeedreadtxt.setText("" + getString(R.string.taskdesk_my_minutesago_count_txt) .replace("$",noreadResponseBean.getCount()+""));
                        }
                    }else{
                        mStateLayout.showSystemView(commonClass.getMsg(),true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                requestDatas();
                            }
                        });
            }
                    break;

            }

        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        requestDatas();
        return null;
    }

    public void onEventMainThread(Object event) {
        if(event instanceof CardDeskMyTabEvent){
            requestDatas();
        }

        if(event instanceof MessageCenterReceivedEvent){
            requestDatas();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
